package short_semester.questionnaire_planet.pojo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import short_semester.questionnaire_planet.entity.User;
import short_semester.questionnaire_planet.request.editRequest.EditQuestionnaireBody;
import short_semester.questionnaire_planet.response.fillResponse.RelationLogicResponse;

import javax.persistence.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;

@Data
@Table(indexes = {@Index(columnList = "img",unique = true)})
@Entity
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })
@EntityListeners(AuditingEntityListener.class)
@NoArgsConstructor
public class Questionnaire {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
    private User user;

    @CreatedDate    //创建日期
    @Column(updatable = false)
    @JsonFormat(shape=JsonFormat.Shape.STRING,pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date createdDate;

    @Column     //第一次发布日期
    @JsonFormat(shape=JsonFormat.Shape.STRING,pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date postDate;

    @Column     //最近一次发布日期
    @JsonFormat(shape=JsonFormat.Shape.STRING,pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date latestPostDate;

    @Column     //最近关闭日期
    @JsonFormat(shape=JsonFormat.Shape.STRING,pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date closeDate;

    @Column     //开始时间
    @JsonFormat(shape=JsonFormat.Shape.STRING,pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date beginDate;

    @Column     //停止时间
    @JsonFormat(shape=JsonFormat.Shape.STRING,pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date endDate;

    @Column     //删除日期
    @JsonFormat(shape=JsonFormat.Shape.STRING,pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date deleteDate;

    @Column     //标题
    private String title = "";

    @Column     //说明
    private String description = "";

    @Column
    private String type = null;

    @Column     //是否发布
    private Boolean isPosted = false;
    

    @Column     //是否删除
    private Boolean isDeleted = false;

    @Column     //是否自动编号
    private Boolean isAutoLabel = true;
    
    
    @Column     //是否需要登陆
    private Boolean isNeedLogin = false;
    
    @Column
    private Boolean isDate = false;

    @Column
    private Boolean isLook = true;

    @Column     //答卷数量
    private Integer count = 0;

    @Column     //随机类型
    private String randomType;
    
    @Column
    private String img;
    
    //考试问卷特有属性
    @Column     //是否自动提交
    private Boolean isAutoSubmit = false;
    
    //报名问卷特有属性
    private Integer store;
    private Integer amount;

    @Transient
    Collection<Object> questions = new LinkedList<>();
    @Transient
    RelationLogicResponse relations;

    public Questionnaire(Integer id) {
        this.id = id;
    }
    
    public Questionnaire(Questionnaire questionnaire){
        title = questionnaire.getTitle()+"-副本";
        description = questionnaire.getDescription();
        isAutoLabel = questionnaire.getIsAutoLabel();
        isNeedLogin = questionnaire.getIsNeedLogin();
        isDate = questionnaire.getIsDate();
        isLook=questionnaire.getIsLook();
        randomType = questionnaire.getRandomType();
        isAutoSubmit = questionnaire.getIsAutoSubmit();
        //拷贝不拷贝现存数量
        amount=questionnaire.getAmount();
        store=amount;
    }
    
    public void setValueFrom(EditQuestionnaireBody body){
        title=body.getTitle();
        description=body.getDescription();
//        type=body.getType();
        randomType=body.getRandomType();
        
        isAutoLabel=body.getIsAutoLabel();
        isNeedLogin=body.getIsNeedLogin();
        isAutoSubmit=body.getIsAutoSubmit();
        isDate = body.getIsDate();
        isLook = body.getIsLook();
        
        //修改会修改现存数量
        amount=body.getStore();
        store=amount;
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            beginDate=sdf.parse(body.getBeginDate());
            endDate=sdf.parse(body.getEndDate());
        }catch (Exception e){
            beginDate=null;
            endDate=null;
        }
    }
}
